﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    [Serializable]
    public class DataQueryRefSchemaEntity : IRefSchemaEntity
    {
        public string ID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public Dictionary<string, DQSource> Sources { get; set; }
        public Dictionary<string, DQColumn> __AggreAndGroupColumns { get; set; }
        public Dictionary<string, DQColumn> __UserDefineColumns { get; set; }
        public Dictionary<string, DQParam> Param { get; set; }
        public Dictionary<string, DQSub> SubDQ { get; set; }

        [Serializable]
        public class DQSource
        {
            public string Code { get; set; }
            public Dictionary<string, DQColumn> Columns { get; set; }
            public Dictionary<string, DQSource> Sources { get; set; }

            public static DQSource Mapping(DataQuery dq, DataQuerySource source)
            {
                if (dq == null || source == null) return default;
                var sour = new DQSource
                {
                    Code = source.Alias,
                    Columns = new Dictionary<string, DQColumn>()
                };
                var curSourceColumns = dq.DataQueryColumns.Where(col => col.DataQuerySourceID == source.ID);
                foreach (var column in curSourceColumns)
                {
                    sour.Columns[column.Alias] = DQColumn.Mapping(dq, column);
                }
                return sour;
            }
        }

        [Serializable]
        public class DQParam
        {
            public string Code { get; set; }
            public string Text { get; set; }
            public string DataType { get; set; }
            public bool IsAllowNull { get; set; }
            public bool IsDefault { get; set; } 

            public static DQParam Mapping(DataQuery dq, DataQueryParam param)
            {
                if (dq == null || param == null) return default;
                var par = new DQParam
                {
                    Code = param.Code,
                    Text = param.Name,
                    DataType = param.ParamType,
                    IsDefault = param.IsDefault,
                    IsAllowNull = param.IsAllowNull,
                };
                return par;
            }
        }

        [Serializable]
        public class DQColumn
        {
            public string Code { get; set; }
            public string Text { get; set; }
            public bool IsHide { get; set; }
            public ColumnType ColumnType { get; set; }
            public string DataType { get; set; }

            public static DQColumn Mapping(DataQuery dq, DataQueryColumn column)
            {
                if (dq == null || column == null) return default;
                var col = new DQColumn
                {
                    Code = column.Alias,
                    Text = column.DisplayName,
                    DataType = column.ColumnDataType,
                    ColumnType = column.ColumnType,
                    IsHide = column.IsHide,
                };
                return col;
            }
        }

        [Serializable]
        public class DQSub
        {
            public string Code { get; set; }

            public static DQSub Mapping(DataQuery dq, DataQuerySubQuery sub)
            {
                if (dq == null || sub == null) return default;
                var su = new DQSub
                {
                    Code = sub.SubQueryAlias,
                };
                return su;
            }
        }
    }
}
